package org.zjt.mock.demo.mock.config;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-06-21 下午9:23
 * @see
 */
@Slf4j
public class CustomizedSerializer {


    /**
     * localDateTime 序列化器
     */
    @Slf4j
    public static class LocalDateTimeSerializer extends StdScalarSerializer<LocalDateTime> {

        private static final DateTimeFormatter DATE_TIME_FORMATTER =  DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        private static final ZoneId zoneId = ZoneId.systemDefault();

        public LocalDateTimeSerializer() { super(LocalDateTime.class, false); }

        @Override
        public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
            String format = value.format(DATE_TIME_FORMATTER);
            jgen.writeStartObject();
            jgen.writeStringField("format", format);
            jgen.writeNumberField("milliseconds", value.atZone(zoneId).toInstant().toEpochMilli());
            jgen.writeEndObject();
        }

    }
}
